﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class DataBaseTree : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
                DropType();
            }
        }

        #region DRPBIND
        private void DropType()
        {
            DrpTopID.Items.Clear();
            List<Sys_BaseDataTreeLev> items = TbsDbSession.Default.From<Sys_BaseDataTreeLev>().Where(Sys_BaseDataTreeLev._.TopID == 0 && Sys_BaseDataTreeLev._.GroupID==int.Parse(Request["gid"])).OrderBy(Sys_BaseDataTreeLev._.OrderID.Asc, Sys_BaseDataTreeLev._.ID.Desc).ToList();
            foreach (Sys_BaseDataTreeLev row in items)
            {
                int count = TbsDbSession.Default.From<Sys_BaseDataTreeLev>().Where(Sys_BaseDataTreeLev._.TopID == row.ID).OrderBy(Sys_BaseDataTreeLev._.OrderID.Asc, Sys_BaseDataTreeLev._.ID.Desc).Count();
                ListItem li = new ListItem();
                li.Text = row.NodeName;
                li.Value = row.ID.ToString();
                //if (count > 0)
                //{
                //    li.EnableSelect = false;

                //}
                DrpTopID.Items.Add(li);
                if (count > 0)
                {
                    ResolveSubTree(row.ID, 0);
                }
            }
            ListItem li2 = new ListItem();
            li2.Text = "--所属类别--";
            li2.Value = "0";
            li2.EnableSelect = false;
            DrpTopID.Items.Insert(0, li2);
        }
        private void ResolveSubTree(int id, int Leve)
        {
            Leve++;
            List<Sys_BaseDataTreeLev> items = TbsDbSession.Default.From<Sys_BaseDataTreeLev>().Where(Sys_BaseDataTreeLev._.TopID == id && Sys_BaseDataTreeLev._.GroupID == int.Parse(Request["gid"])).OrderBy(Sys_BaseDataTreeLev._.OrderID.Asc, Sys_BaseDataTreeLev._.ID.Desc).ToList();
            if (items.Count > 0)
            {
                foreach (Sys_BaseDataTreeLev row in items)
                {
                    int count = TbsDbSession.Default.From<Sys_BaseDataTreeLev>().Where(Sys_BaseDataTreeLev._.TopID == row.ID).OrderBy(Sys_BaseDataTreeLev._.OrderID.Asc, Sys_BaseDataTreeLev._.ID.Desc).Count();
                    ListItem li = new ListItem();
                    li.Text = row.NodeName;
                    li.Value = row.ID.ToString();
                    li.SimulateTreeLevel = Leve;
                    //if (count > 0)
                    //{
                    //    li.EnableSelect = false;
                    //}
                    DrpTopID.Items.Add(li);
                    if (count > 0)
                    {
                        ResolveSubTree(row.ID, Leve);
                    }

                }
            }

        }
        #endregion

        #region TREE
        private void BindTree()
        {
            MgrTree.Nodes.Clear();
            List<Sys_BaseDataTreeLev> items = TbsDbSession.Default.From<Sys_BaseDataTreeLev>().Where(Sys_BaseDataTreeLev._.GroupID == int.Parse(Request["gid"]) && Sys_BaseDataTreeLev._.TopID == 0).OrderBy(Sys_BaseDataTreeLev._.OrderID.Asc, Sys_BaseDataTreeLev._.ID.Desc).ToList();

            foreach (Sys_BaseDataTreeLev row in items)
            {
                TreeNode node = new TreeNode();
                node.Text = row.NodeName;
                node.NodeID = row.ID.ToString();
                node.Expanded = true;
                node.EnablePostBack = true;
                MgrTree.Nodes.Add(node);
                ResolveSubTree(row.ID, node);
            }

        }

        private void ResolveSubTree(int id, TreeNode treeNode)
        {

            List<Sys_BaseDataTreeLev> items = TbsDbSession.Default.From<Sys_BaseDataTreeLev>().Where(Sys_BaseDataTreeLev._.TopID == id).OrderBy(Sys_BaseDataTreeLev._.OrderID.Asc, Sys_BaseDataTreeLev._.ID.Desc).ToList();

            if (items.Count > 0)
            {
                treeNode.Expanded = true;
                foreach (Sys_BaseDataTreeLev row in items)
                {
                    TreeNode node = new TreeNode();
                    node.Text = row.NodeName;
                    node.NodeID = row.ID.ToString();
                    node.Expanded = true;
                    node.EnablePostBack = true;
                    treeNode.Nodes.Add(node);
                    ResolveSubTree(row.ID, node);
                }
            }
        }
        #endregion


        protected void btnDel_Click(object sender, EventArgs e)
        {
            if (MgrTree.SelectedNodeID != "")
            {
                int id = int.Parse(MgrTree.SelectedNodeID);
                TbsDbSession.Default.Delete<Sys_BaseDataTreeLev>(Sys_BaseDataTreeLev._.ID == id);
                BindTree();
                DropType();
            }
        }

        protected void MgrTree_NodeCommand(object sender, ChanExt.TreeCommandEventArgs e)
        {

        }

        protected void btnTop_Click(object sender, EventArgs e)
        {
            txt_NodeName.Text = "";
            txt_NodeValue.Text = "";
            txt_OrderID.Text = "0";
            txt_ReMark.Text = "";
            DrpTopID.SelectedValue = "0";
            PageContext.RegisterStartupScript(Window1.GetShowReference());
        }

        protected void btnChild_Click(object sender, EventArgs e)
        {
            txt_NodeName.Text = "";
            txt_NodeValue.Text = "";
            txt_OrderID.Text = "0";
            txt_ReMark.Text = "";
            DrpTopID.SelectedValue = MgrTree.SelectedNodeID;
            PageContext.RegisterStartupScript(Window1.GetShowReference());
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (MgrTree.SelectedNodeID != "")
            {
                int id = int.Parse(MgrTree.SelectedNodeID);
                hid_ID.Text = id.ToString();
                Sys_BaseDataTreeLev tree = TbsDbSession.Default.From<Sys_BaseDataTreeLev>().Where(Sys_BaseDataTreeLev._.ID == id).ToFirst();
                txt_NodeName.Text = tree.NodeName;
                txt_NodeValue.Text = tree.NodeValue;
                txt_OrderID.Text = tree.OrderID.Value.ToString();
                txt_ReMark.Text = tree.ReMark;
                DrpTopID.SelectedValue = tree.TopID.Value.ToString();
                PageContext.RegisterStartupScript(Window1.GetShowReference());
            }
        }

        protected void btn_NodeSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Sys_BaseDataTreeLev treelev = new Sys_BaseDataTreeLev();
            if (hid_ID.Text != "")
            {
                treelev.ID = int.Parse(hid_ID.Text);
                treelev.Attach();
                treelev.GroupID = int.Parse(Request["gid"]);
                treelev.NodeName = txt_NodeName.Text;
                treelev.NodeValue = txt_NodeValue.Text;
                treelev.OrderID = int.Parse(txt_OrderID.Text);
                treelev.ReMark = txt_ReMark.Text;
                treelev.TopID = int.Parse(DrpTopID.SelectedValue);
                returnvalue = TbsDbSession.Default.Update<Sys_BaseDataTreeLev>(treelev);
            }
            else
            {
                treelev.DeAttach();
                treelev.GroupID = int.Parse(Request["gid"]);
                treelev.NodeName = txt_NodeName.Text;
                treelev.NodeValue = txt_NodeValue.Text;
                treelev.OrderID = int.Parse(txt_OrderID.Text);
                treelev.ReMark = txt_ReMark.Text;
                treelev.TopID = int.Parse(DrpTopID.SelectedValue);
                returnvalue = TbsDbSession.Default.Insert<Sys_BaseDataTreeLev>(treelev);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", Window1.GetHideRefreshReference());
               // BindTree();
               // DropType();
            }
        }
    }
}